/*
 * Copyright DimYLiK.
 */

package com.example.myapp.utilities;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

public class XMLReaderWriter {

    private static final String XML_VOCABULARY_PATH = "WEB-INF/vocabulary.xml";
    private static final String NEW_XML_VOCABULARY_PATH = "new_vocabulary.xml";
    private static final String ENTRY = "entry";
    private static final String WORD = "word";
    private static final String DESCRIPTION = "descr";

    public static void main(String... args) {

        try {
            File file = new File(XML_VOCABULARY_PATH);
            DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document document = documentBuilder.parse(new FileInputStream(file), "UTF-8");
            document.getDocumentElement().normalize();
            System.out.println("Root element " + document.getDocumentElement().getNodeName());
            NodeList nodeList = document.getElementsByTagName(ENTRY);

            FileWriter fileWriter = new FileWriter(new File(NEW_XML_VOCABULARY_PATH));
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            bufferedWriter.newLine();
            bufferedWriter.write("<vocabulary>");
            bufferedWriter.newLine();

            for (int s = 0; s < nodeList.getLength(); s++) {
//            for (int s = 0; s < 10; s++) {
                Node node = nodeList.item(s);
                if (Node.ELEMENT_NODE == node.getNodeType()) {
                    Element element = (Element) node;
                    String word = getEntryByTag(element, WORD);
                    int len = word.length();
                    if (isWordLenCorrect(len) && word.indexOf(".") < 0) {
                        String description = getEntryByTag(element, DESCRIPTION);
                        bufferedWriter.write("<entry><word>" + word.trim() + "</word><descr>" + description.trim() + "</descr></entry>");
                        bufferedWriter.newLine();
//                        System.out.println(word + " : " + description);
                    }
                }
            }
            bufferedWriter.write("</vocabulary>");
            bufferedWriter.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static boolean isWordLenCorrect(int len) {
        return len <= 12 && len >= 3;
    }

    private static String getEntryByTag(Element fstElmnt, String tag) {
        NodeList nodeList = fstElmnt.getElementsByTagName(tag);
        Element element = (Element) nodeList.item(0);
        NodeList childNodes = element.getChildNodes();
        return childNodes.item(0).getNodeValue();
    }
}
